package cn.wolfcode.p2p.controller;

import cn.wolfcode.p2p.base.exception.CustomerException;
import cn.wolfcode.p2p.base.service.ILoginInfoService;
import cn.wolfcode.p2p.base.service.ISendVerifyCodeService;
import cn.wolfcode.p2p.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 前台注册逻辑
 */
@Controller
public class RegisterController {
    @Autowired
    private ILoginInfoService loginInfoService;
    @Autowired
    private ISendVerifyCodeService sendVerifyCodeService;

    /**
     * 前台注册
     */
    @RequestMapping("userRegister")
    @ResponseBody
    public JsonResult register(String username, String verifycode, String password, String confirmPwd) {

        JsonResult jsonResult = new JsonResult();
        try {
            loginInfoService.register(username, verifycode, password, confirmPwd);
        } catch (CustomerException e) {
            e.printStackTrace();
            //客户自身问题
            jsonResult.setMessage(e.getMessage());
        } catch (Exception e) {
            //系统本身问题
            jsonResult.setMessage("系统繁忙中,请稍后再试");
        }
        return jsonResult;
    }

    /**
     * 判断手机号是否被注册
     * @param username
     * @return
     */
    @RequestMapping("existUsername")
    @ResponseBody
    public boolean existUsername(String username){
        return !(loginInfoService.existUsername(username));
    }

    /**
     * 获取验证码
     * @param phone 用户手机号
     * @return
     */
    @RequestMapping("sendVerifyCode")
    @ResponseBody
    public JsonResult sendVerifyCode(String phone){
        JsonResult jsonResult = new JsonResult();
        try {
            sendVerifyCodeService.sendVerifyCode(phone);
        }catch (CustomerException e){
            jsonResult.setMessage(e.getMessage());
            throw new CustomerException(e.getMessage());
        }
        catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("系统繁忙中,请稍后再试");
        }
        return jsonResult;
    }

}
